import glob
import os
import tkinter as tk
from tkinter import messagebox
from PIL import Image, ImageTk, ImageDraw
import keyboard
import math

def create_window(parent):
    global offX, offY
    offX = 60
    offY = 50

    # 裁剪图片边界区域
    global left_value, right_value, up_value, down_value
    left_value = 1
    right_value = 1
    up_value = 1
    down_value = 1

    def find_png_files(directory):
        png_files = glob.glob(os.path.join(directory, '**', '*.png'), recursive=True)
        return png_files

    # 创建窗口
    root = tk.Toplevel(parent)
    root.title("序列帧图片裁剪器")
    root.config(bg="grey")
    root.geometry("1000x1000+0+0")

    global imgs_scale, scale_value, scale_index
    scale_value = 1
    imgs_scale = []
    scale_index = 0

    # 裁剪图片路径输入框
    entry_scale_change_path = tk.StringVar()
    entry_path = tk.Entry(root, textvariable=entry_scale_change_path)
    entry_path.place(x=offX + 40, y=offY - 37)

    # 图像绘制位置坐标
    canvas_scale = tk.Canvas(root, width=800, height=800, bg="gray")
    canvas_scale.place(x=offX + 70, y=offY + 100)

    def load_images():
        global imgs_scale, button_scale
        global button_scale_load, button_scale_sure, scale_value, input_folder, output_folder, imgs_scale_path
        imgs_scale_path = []
        input_folder = entry_scale_change_path.get()
        # output_folder = input_folder + "\\output"#r"D:\Users\admin\Desktop\File\130001\output"

        # if not os.path.exists(output_folder):
        #     os.makedirs(output_folder)

        for filename in os.listdir(input_folder):
            if filename.endswith('.png') or filename.endswith('.jpg'):
                imgs_scale_path.append(
                    os.path.join(input_folder, filename))  # = [Image.open(os.path.join(input_folder, filename))]

        imgs_scale = [Image.open(path) for path in imgs_scale_path]
        update_frame_sclae()

    # 预览输入框
    entry_scale_change = tk.StringVar()
    entry_path = tk.Entry(root, textvariable=entry_scale_change, width=5)
    entry_path.place(x=offX + 300, y=offY - 37)

    def get_scale_value():
        global scale_value
        scale_value = 0
        scale_value = float(entry_scale_change.get())
        # update_frame_sclae()

    button_scale_sure = tk.Button(root, text="缩放大小预览", command=get_scale_value, bg='skyblue').place(x=offX + 350,
                                                                                                          y=offY - 40)
    button_scale = tk.Button(root, text="加载图片", command=load_images, bg='skyblue').place(x=offX + 200, y=offY - 40)

    def get_tailor_value():
        global button_tailor_left, button_tailor_right, button_tailor_up, button_tailor_down, imgs_scale
        global left_value, right_value, up_value, down_value, resized_img
        if (entry_tailor_left.get() != ""):
            left_value = int(entry_tailor_left.get())
        if (entry_tailor_right.get() != ""):
            right_value = resized_img.size[0] - int(entry_tailor_right.get())
        else:
            right_value = imgs_scale[0].width
        if (entry_tailor_up.get() != ""):
            up_value = int(entry_tailor_up.get())
        if (entry_tailor_down.get() != ""):
            down_value = resized_img.size[1] - int(entry_tailor_down.get())
        else:
            down_value = imgs_scale[0].height
        # update_frame_sclae()

    # 裁剪left输入框
    entry_tailor_left = tk.StringVar()
    entry_path = tk.Entry(root, textvariable=entry_tailor_left, width=5)
    entry_path.place(x=offX + 50, y=offY + 60)
    # button_tailor_left = tk.Button(root, text="确认",command=get_tailor_value).place(x=offX + 990, y=offY + 57)
    tk.Label(root, text="左边界", bg="green").place(x=offX + 50, y=offY + 30)

    # 裁剪right输入框
    entry_tailor_right = tk.StringVar()
    entry_path = tk.Entry(root, textvariable=entry_tailor_right, width=5)
    entry_path.place(x=offX + 100, y=offY + 60)
    # button_tailor_right = tk.Button(root, text="确认",command=get_tailor_value).place(x=offX + 1090, y=offY + 57)
    tk.Label(root, text="右边界", bg="green").place(x=offX + 100, y=offY + 30)

    # 裁剪up输入框
    entry_tailor_up = tk.StringVar()
    entry_path = tk.Entry(root, textvariable=entry_tailor_up, width=5)
    entry_path.place(x=offX + 150, y=offY + 60)
    # button_tailor_up = tk.Button(root, text="确认",command=get_tailor_value).place(x=offX + 1190, y=offY + 57)
    tk.Label(root, text="上边界", bg="green").place(x=offX + 150, y=offY + 30)

    # 裁剪down输入框
    entry_tailor_down = tk.StringVar()
    entry_path = tk.Entry(root, textvariable=entry_tailor_down, width=5)
    entry_path.place(x=offX + 200, y=offY + 60)
    button_tailor_down = tk.Button(root, text="裁剪区域预览", command=get_tailor_value).place(x=offX + 290, y=offY + 57)
    tk.Label(root, text="下边界", bg="green").place(x=offX + 200, y=offY + 30)

    # 裁剪图片指定边界
    def tailor():
        global left_value, right_value, up_value, down_value, resized_img, imgs_scale, img

        finally_Names = input_folder.split("\\")
        finally_Name = finally_Names[len(finally_Names) - 1]
        output_folder = input_folder + "(new)"  # r"D:\Users\admin\Desktop\File\130001\output"
        if not os.path.exists(output_folder):
            os.makedirs(output_folder)

        for img in imgs_scale:
            name = img.filename.split("\\")[len(img.filename.split("\\")) - 1]
            img_crop = img.crop((left_value, up_value, right_value, down_value))
            img_crop.save(os.path.join(output_folder, name))

    button_tailor = tk.Button(root, text="保存图片", command=tailor, bg="skyblue").place(x=offX + 480, y=offY - 40)

    # 刷新帧率图片
    def update_frame_sclae():
        global imgs_scale, scale_index, photo, resized_img, name, root_after2
        if imgs_scale:
            canvas_scale.delete("all")
            length = len(imgs_scale)
            img_scale = imgs_scale[scale_index]
            name = img_scale.filename.split("\\")[len(img_scale.filename.split("\\")) - 1]
            resize = [math.ceil(img_scale.size[0] * scale_value), math.ceil(img_scale.size[1] * scale_value)]
            resized_img = img_scale.resize(resize, Image.Resampling.LANCZOS)
            photo = ImageTk.PhotoImage(resized_img)
            x = int(photo.width() / 2)
            y = int(photo.height() / 2)

            canvas_scale.create_rectangle(x - photo.width() / 2, y - photo.height() / 2, x + photo.width() / 2,
                                          y + photo.height() / 2, fill="white", outline="black")
            canvas_scale.create_image(x, y, anchor=tk.CENTER, image=photo)

            canvas_scale.create_line(left_value, up_value, right_value, up_value, fill="red")
            canvas_scale.create_line(left_value, down_value, right_value, down_value, fill="red")
            canvas_scale.create_line(left_value, up_value, left_value, down_value, fill="red")
            canvas_scale.create_line(right_value, up_value, right_value, down_value, fill="red")

            scale_index = (scale_index + 1) % length  # 循环显示
        root_after2 = root.after(200, update_frame_sclae)

